package cn.agiledata.bank.myspace.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.agiledata.bank.common.servlet.InitForString;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.hibernateBean.AccountLimit;
import cn.agiledata.bank.hibernateBean.Acctlimithis;
import cn.agiledata.bank.hibernateBean.ActionType;
import cn.agiledata.bank.hibernateBean.GradeRules;
import cn.agiledata.bank.hibernateBean.Groups;
import cn.agiledata.bank.hibernateBean.MessageCustomization;
import cn.agiledata.bank.hibernateBean.OperSubacct;
import cn.agiledata.bank.hibernateBean.Operator;
import cn.agiledata.bank.hibernateBean.Sysbook;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class MySpaceDaoImpl extends IPBHibernate3BaseMgr implements IMySpaceDao {
	/**
	 * 根据操作者id得到该操作者 add by cjh
	 * 
	 * @param id
	 * @return
	 */
	public Operator getOperatorById(String id) {
		return (Operator) getHibernateTemplate().get(Operator.class,
				new Long(id));
	}

	/**
	 * 银行账户信息
	 */
	public List getAccountBankList(String accountID) {
		String hsql = "from AccountBank bank where " + "bank.account.id="
				+ accountID
				+ " and (bank.isValid='Y' or bank.isValid is null) order by "
				+ "bank.createTime desc";
		return getHibernateTemplate().find(hsql);
	}

	/**
	 * 获取交易对象列表
	 * 
	 * @param accountId
	 * @return
	 * @author cyg
	 */
	public List getTrxObjectList(String accountId) {
		String hsql = "from PeerInnerAccount pah where pah.account.id = ? order by pah.updateTime desc";
		return getHibernateTemplate().find(hsql, new Long(accountId));
	}

	/**
	 * 修改操作者 add by cjh
	 * 
	 * @param id
	 * @return
	 */
	public void updateOperator(Operator oper) {
		getHibernateTemplate().update(oper);
	}

	/**
	 * 是否唯一
	 */
	public boolean isUniqueSubAccountField(String value, String dbName,
			String createUser) {
		// TODO 自动生成方法存根
		StringBuffer sb = new StringBuffer();
		sb.append(" from SubAccount where ");
		sb.append(dbName);
		sb.append(" = ? and validFlag='Y' and createUser =? ");
		List list = getHibernateTemplate().find(sb.toString(),
				new Object[] { value.trim(), createUser });
		if (list != null) {
			if (list.size() == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public boolean isUniqueSubAccountFieldExcludeOwn(String value,
			String dbName, Long id, String createUser) {
		StringBuffer hql = new StringBuffer();
		hql.append("from SubAccount where ").append(dbName).append(" = ? ")
				.append(" and createUser =? and id <> ? and validFlag='Y'");
		List list = getHibernateTemplate().find(hql.toString(),
				new Object[] { value.trim(), id, createUser });
		if (list != null) {
			if (list.size() == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	/* 更新一条acctlimit的记录* */
	public void updateAcctLimit(AccountLimit al) {
		getHibernateTemplate().update(al);
	}

	/* 增加一条acctlimit的纪录* */
	public void saveAcctLimit(AccountLimit al) {
		getHibernateTemplate().save(al);
	}

	/* 根据account.id查询一条acctlimit* */
	public AccountLimit getAcctlimitByAcctId(String id) {
		String hql = "from AccountLimit a where a.account.id = " + id;
		List list = getHibernateTemplate().find(hql);
		Iterator it = list.iterator();
		Object a = null;
		while (it.hasNext()) {
			a = it.next();
		}
		if (a == null) {
			return null;
		} else {
			return (AccountLimit) a;
		}

	}

	/* 根据客户类别查询默认的acctlimit* */
	public AccountLimit getAcctlimitByAcctGrading(String grading) {
		String hql = "from AccountLimit a where a.limitType = '1' and a.groups.id = "
				+ grading;
		List list = getHibernateTemplate().find(hql);
		Iterator it = list.iterator();
		Object a = null;
		while (it.hasNext()) {
			a = it.next();
		}
		if (a == null) {
			return null;
		} else {
			return (AccountLimit) a;
		}
	}

	public void updateAllAccount() {
		// TODO Auto-generated method stub
		String hql = "update Account set sumAmount = 0 , tranAmount = 0";
		executeUpdate(hql);
		// 为所有账户解除锁定
		hql = "update Account set status='" + Constant.ACCOUNT_STATE_NORMAL
				+ "' where status='" + Constant.ACCOUNT_STATE_LOGIN_LOCK + "'";
		executeUpdate(hql);
	}

	public GradeRules getGradingRulesByGrading(String gradingId) {
		String hql = "from GradeRules t where t.groupsByGrading.id = "
				+ gradingId;
		List list = getHibernateTemplate().find(hql);
		Iterator it = list.iterator();
		Object a = null;
		while (it.hasNext()) {
			a = it.next();
		}
		if (a == null) {
			return null;
		} else {
			return (GradeRules) a;
		}
	}

	public int executeUpdate(final String queryString)
			throws DataAccessException {
		HibernateTemplate ht = getHibernateTemplate();
		return ((Integer) ht.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				// checkWriteOperationAllowed(session);
				Query queryObject = session.createQuery(queryString);
				return Integer.valueOf(queryObject.executeUpdate() + "");
			}
		}, true)).intValue();
	}

	public void saveAcctLimithis(Acctlimithis als) {

		getHibernateTemplate().save(als);
	}

	public List getAuthenticationById(String id) {
		String hql = "from Authentication t where t.gradingRules.id = " + id;
		return getHibernateTemplate().find(hql);
	}

	/**
	 * 获得认证方式
	 */
	public List getAuthenticationMethod() {
		return InitForString
				.getTypeList(Constant.SYSBOOK_AUTHENTICATION_METHOD);
		// String hql = "from Sysbook s where s.typeId = '"+
		// Constant.SYSBOOK_AUTHENTICATION_METHOD +"' "
		// + "and s.id.listId != '3A' " + "and s.id.listId !='3B' "
		// + "and s.id.listId != '3C' ";
		// return getHibernateTemplate().find(hql);
	}

	/**
	 * 获取所有可定制消息
	 */
	public List getAllMessageCustoms() {
		String hql = "from MessageCustomization m";
		return getHibernateTemplate().find(hql);

	}

	/**
	 * 获得银行账户类别
	 */
	public String getCardType(String id) {
		return ((Sysbook) InitForString.getTypeList(Constant.SYSBOOK_CARD_TYPE)
				.get(0)).getListName();
		// String hql = "from Sysbook s where s.typeId='"+
		// Constant.SYSBOOK_CARD_TYPE +"' and s.id.listId = "+id;
		// Sysbook book = (Sysbook) getHibernateTemplate().find(hql).get(0);
		// return book.getListName();
	}

	public Groups getGroupsById(String id) {
	    String hql = "from Groups t where t.id = '"+id+"'";
		return (Groups)getHibernateTemplate().find(hql).get(0);
	}
	
	public List getAuditOperater(String id) {
		String hql = "from Operator t where t.valid = true and t.account.id = "+id;
		List list = this.getHibernateTemplate().find(hql);
		return list;
	}

	public List getAuditGroup(String accountId) {
		String hql = "from AuditGroup t where  t.account.id = "+accountId;
		List list = this.getHibernateTemplate().find(hql);
		return list;
	}

	public OperSubacct getOperSubacct(String opId, String subId) {
		String hql = "from OperSubacct t where t.id.subAccount.id = '"+subId+"' and t.id.operator.id = '"+opId+"' and t.id.sysbook.id.listId = '5'";
		List list = this.getHibernateTemplate().find(hql);
		if(list.size()>0)
			return (OperSubacct)list.get(0);
		else
			return null;
	}

	public ActionType getActionTypeById(String id) {
		String hql = "from ActionType t where t.id = '"+id+"'";
		List list = this.getHibernateTemplate().find(hql);
		if(list.size()>0)
			return (ActionType)list.get(0);
		else
			return null;
	}
	//得到所有的审核员
	public List getAuditOperaters(String id) {
		String hql = "from Operator t where t.valid = true and t.auditFlag = 'Y' and t.account.id = "+id;
		List list = this.getHibernateTemplate().find(hql);
		return list;
	}

	public List getAccountMessage(String accountId, String messageCustomizationId) {
		 String hql = "from AccountMessage t where t.messageCustomization.id = '"
             + messageCustomizationId
             + "' and t.account.id = '"
             + accountId + "'";
     List list = getHibernateTemplate().find(hql);
     return list;
	}

	public MessageCustomization getMessageCustomization(Long id) {
		
		return (MessageCustomization) getHibernateTemplate().get(MessageCustomization.class, id);
	}
	
	public List getOper(String operSeq,String status){
		String hql = "from EsPrePayTrans t where t.operatorSeq = '"+operSeq+"' and t.transStatus!= '"+Constant.ES_TRANS_STATUS_CANCEL+"'  and t.transStatus != '"+status+"'";
		return getHibernateTemplate().find(hql);
	}
}
